﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using CRM.Database.Model;
using DevExpress.Web.Mvc;


namespace CRM.Controllers
{
    [Localize]
    public class CustomersController : Controller
    {
        #region Customers

        public ActionResult Index()
        {
            var col=new CustomersCollection();
            return View(col.ActiveCustomers);
        }


        public ActionResult GetEquipEditCallBack()
        {
            var col = new EquipmentsCollection();
            return PartialView("EquipmentList",col.EquipmentsList);
        }

        public ActionResult AllCustomersNames()
        {
            var col = new CustomersNamesCollection();
            return View(col.CustomersNamesList);
        }

        public ActionResult EquipmentsGridViewPartial()
        {
           // string test = Request.Params["selectedValues"];
            var col = new EquipmentsCollection();
            return PartialView("EquipmentsListPartialView", col.EquipmentsList);
        }

        public ActionResult CustomersPartional()
        {
            var col = new CustomersCollection();
            return PartialView("CustomersPartional", col.ActiveCustomers);
        }

        public ActionResult CustomersNamesPartional()
        {
            var col = new CustomersNamesCollection();
            return PartialView("CustomersNamesPartial", col.CustomersNamesList);
        }

        public ActionResult Details(int id)
        {
            var col = new CustomersCollection();
            int idCustomer = id;
           CRM.Database.Model.Customers customers = (Database.Model.Customers) col.GetItemById(idCustomer);
            return View(customers);
        }

        public ActionResult Edit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return View(customers);
        }

        public ActionResult GeneralEdit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("GeneralEdit", customers);
        }

        [HttpGet]
        public ActionResult NewCustomer()
        {
            var customer = new Customers();
            return View(customer);
        }   

        [HttpPost]
        public ActionResult NewCustomer([ModelBinder(typeof(DevExpressEditorsBinder))] Database.Model.Customers newCustomer)
        {
            //if(newCustomer.IsTheSameCustomerExists)
            //{
                
            //}
            var col = new CustomersCollection();
            if (!col.Add(newCustomer, Request.LogonUserIdentity.Name))
                return RedirectToAction("Error", "Home");
            int idCust = newCustomer.IdCustomer;
            return RedirectToAction("Details", new RouteValueDictionary(new { id = idCust }));
            
        }

        [HttpPost]
        public ActionResult Edit(Customers customers)
        {
          //  customers.IdUser = Request.LogonUserIdentity.Name;

           if(true)//ModelState.IsValid)
           {

               var col = new CustomersCollection();

               if(customers.IdCustomer==0)
               {
                   customers.IdCustomer = col.GetCustomerBySequentalNumber(customers.SequentialNumber).IdCustomer;
               }

               int idProfineRegion = 0;
               if (Request.Params["IdprofineRegion_VI"] != null)
               {
                   Int32.TryParse(Request.Params["IdprofineRegion_VI"], out idProfineRegion);
                   customers.IdprofineRegion = idProfineRegion;
               }

               int idCustomerClassificationsProfine = 0;
               if (Request.Params["IdCustomerClassificationsProfine_VI"] != null)
               {
                   Int32.TryParse(Request.Params["IdCustomerClassificationsProfine_VI"], out idCustomerClassificationsProfine);
                   customers.IdCustomerClassificationsProfine = idCustomerClassificationsProfine;
               }

               int idManager = 0;
               if (Request.Params["IdManager_VI"] != null)
               {
                   Int32.TryParse(Request.Params["IdManager_VI"], out idManager);
                   customers.IdManager = idManager;
               }

               int idCustomerClassificationsByBusiness = 0;
               if (Request.Params["IdCustomerClassificationsByBusiness_VI"] != null)
               {
                   Int32.TryParse(Request.Params["IdCustomerClassificationsByBusiness_VI"], out idCustomerClassificationsByBusiness);
                   customers.IdCustomerClassificationsByBusiness = idCustomerClassificationsByBusiness;
               }


               int idCustomerClassificationsTurnover = 0;
               if (Request.Params["IdCustomerClassificationsTurnover_VI"] != null)
               {
                   Int32.TryParse(Request.Params["IdCustomerClassificationsTurnover_VI"], out idCustomerClassificationsTurnover);
                   customers.IdCustomerClassificationsTurnover = idCustomerClassificationsTurnover;
               }

              
               if(col.Update(customers,Request.LogonUserIdentity.Name))
             //  DB.CustomersUpdate(customers, Request.LogonUserIdentity.Name);
               {
                   
                    return RedirectToAction("Details", new RouteValueDictionary(new { id = col.GetCustomerBySequentalNumber(customers.SequentialNumber).IdCustomer }));
               }
               return RedirectToAction("Error","Home");
           }
          //  RedirectToAction("Edit", new { id = customers.IdCustomer });
       //    return View("Details", col.GetCustomerBySequentalNumber(customers.SequentialNumber));
           // return View("Edit", DB.GetCustomerBySequentalNumber(customers.SequentialNumber));
            //   return RedirectToAction("Index");

            // return View("Details", DB.GetCustomerBySequentalNumber(customers.SequentialNumber));
            //  return Details(DB.GetCustomerBySequentalNumber(customers.SequentialNumber).IdCustomer);

        }

        public ActionResult Turnovers(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("Turnover", customers);
        }

        public ActionResult SalesTurnovers(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("SalesTurnovers", customers);
        }

        public ActionResult AgreementElements(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("AgreementElements",customers);
        }

        public ActionResult AgreementElementsEdit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("AgreementElementsEdit", customers);
        }

        public ActionResult GetDistributionChanels(Database.Model.Customers customers)
        {
            // throw new NotImplementedException();
            return PartialView("General_DistributionChanels", customers);
        }

        public ActionResult GetGeneral_ProductPortfolio(Database.Model.Customers customers)
        {
            return PartialView("General_ProductPortfolio", customers);
        }

        public ActionResult GetAddressesAndContacts_Contacts(Database.Model.Customers customers)
        {
            return PartialView("AddressesAndContacts_Contacts", customers);
        }

        public ActionResult GetAddressesAndContacts_Contacts_Edit(Database.Model.Customers customers)
        {
            return PartialView("AddressesAndContacts_Contacts_Edit", customers);
        }

        public ActionResult GetAddressesAndContacts_Address(Database.Model.Customers customers)
        {
            return PartialView("AddressesAndContacts_Address", customers);
        }

        public ActionResult SaveCustomer(string s)
        {
            return View("Index");
        }
 
        #endregion

        #region Equipments

        public ActionResult CustomerEquipments(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("Equipment_Equipment", customers);
        }

        public ActionResult CustomerEquipments_Edit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("Equipment_Equipment_Edit", customers);
        }
       
        
        [HttpPost]
        public ActionResult CustomerEquipmentsAdd([ModelBinder(typeof(DevExpressEditorsBinder))] CustomersEquipments obj, int customerId)
        {


            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);
            int idEquipment = 0;
            Int32.TryParse(Request.Params["ddeEquipmentKV"], out idEquipment);
            if (idEquipment > 0)
            {
                obj.IdEquipment = idEquipment;
            }
            obj.IdCustomer = customerId;


            var colEq = new CustomersEquipmentsCollection();
            colEq.Add(obj, Request.LogonUserIdentity.Name);
          

            return PartialView("Equipment_Equipment_Edit",customers);
        }

        [HttpPost]
        public ActionResult CustomerAddressesAndContacts_Addresses_Add([ModelBinder(typeof(DevExpressEditorsBinder))] Addresses item, int customerId)
        {


            //int idSettlement = 0;
            //Int32.TryParse(Request.Form["Settlement_VI"].ToString(), out idSettlement);
            //if (idSettlement != 0)
            //    item.IdSettlement = idSettlement;
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);

            var addrCol = new AddressesCollection();
            if (!addrCol.Add(item, Request.LogonUserIdentity.Name))
                return RedirectToAction("Error", "Home");
            //customers.AddAddress(item, Request.LogonUserIdentity.Name);


            return PartialView("GetAddressesAndContacts_Address_Edit", customers);
        }


        /// <summary>
        /// Удаление адреса
        /// </summary>
        /// <param name="item">Адрес</param>
        /// <param name="customerId">Идентификатор клиента</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CustomerAddressesAndContacts_Addresses_Delete([ModelBinder(typeof(DevExpressEditorsBinder))] Addresses item, int customerId)
        {


            var col = new CustomersCollection();
            var addrCol = new AddressesCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);

           if(!addrCol.Delete(item))
           {
               return RedirectToAction("Error", "Home");
           }


            return PartialView("GetAddressesAndContacts_Address_Edit", customers);
        }


        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerAddressesAndContacts_Addresses_Update([ModelBinder(typeof(DevExpressEditorsBinder))] Addresses item, int customerId)
        {
           // int idSettlement = 0;
          

           //if(idSettlement!=0)
           //    item.IdSettlement = idSettlement;
            var col = new CustomersCollection();
            int idCustomer = customerId;
            Customers customers = (Customers)col.GetItemById(idCustomer);

            var addrCol = new AddressesCollection();

            addrCol.Update(item, Request.LogonUserIdentity.Name);

         //   customers.UpdateAddress(item, Request.LogonUserIdentity.Name);

            return PartialView("GetAddressesAndContacts_Address_Edit", customers);
        }

        public ActionResult CustomerEquipmentsDelete([ModelBinder(typeof(DevExpressEditorsBinder))] CustomersEquipments item, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            var customers = (Customers)col.GetItemById(idCustomer);
            var colEq = new CustomersEquipmentsCollection();
            colEq.Delete(item);
            //customers.RemoveEquipment(item);

            return PartialView("Equipment_Equipment_Edit", customers);
        }

        [HttpPost]
        public ActionResult CustomerEquipmentsUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] CustomersEquipments obj, int customerId)
        {
            int idEquipment=0;
            Int32.TryParse(Request.Params["ddeEquipmentKV"], out idEquipment);
            if(idEquipment>0)
            {
                obj.IdEquipment = idEquipment;
            }
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers customers = (Database.Model.Customers)col.GetItemById(idCustomer);

            if(obj.IdCustomer==0)
                obj.IdCustomer = customerId;

           

            var colEq = new CustomersEquipmentsCollection();

            if (obj.IdEquipment == 0)
            {
                if (obj.IdCustomersEquipments != 0)
                    obj.IdEquipment = ((CustomersEquipments) colEq.GetItemById(obj.IdCustomersEquipments)).IdEquipment;
            }

            colEq.Update(obj, Request.LogonUserIdentity.Name);

            return PartialView("Equipment_Equipment_Edit", customers);
        }

        public ActionResult GetEquipment_Equipment(Database.Model.Customers customers)
        {
           
            return PartialView("Equipment_Equipment", customers);
        }

        public ActionResult GetEquipment_Tools(Database.Model.Customers customers)
        {
            return PartialView("Equipment_Tools", customers);
        }

        public ActionResult GetEquipment_Furnuture(Database.Model.Customers customers)
        {
            return PartialView("Equipment_Furnuture", customers);
        }

        public ActionResult Equipment_Steklo(Database.Model.Customers customers)
        {
            return PartialView("Equipment_Steklo", customers);
        }

        public ActionResult GetEquipment_Equipment_Edit(Database.Model.Customers customers)
        {
            return PartialView("Equipment_Equipment_Edit", customers);
        }

        #endregion

        #region EquipmentTools
        
        /// <summary>
        /// Инструменты клиента
        /// </summary>
        /// <param name="customerId">Идентификатор клиента</param>
        /// <returns>Инструменты клиенты</returns>
        public ActionResult CustomerEquipmentsTools(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("Equipment_Tools", currentCustomer);
        } 
        #endregion

        #region LegalEntities
       
        public ActionResult CustomerLegalEntities(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("LegalEntities", currentCustomer);
        }

        public ActionResult CustomerLegalEntities_Edit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("LegalEntitiesEdit", currentCustomer);
        }

        public ActionResult CustomerLegalEntitiesUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] LegalEntities legalEntity, int customerId)
        {
           // throw new NotImplementedException();
            var col = new CustomersCollection();
            int idCustomer = customerId;
            legalEntity.IdCustomer = idCustomer;
            legalEntity.IsActive = true;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            var leCol = new LegalEntitiesCollection();

            leCol.Update(legalEntity, Request.LogonUserIdentity.Name);

            //LegalEntities le4update = DB.GetLegalEntityById(legalEntity.IdLegalEntity);
            //le4update.Update(legalEntity, Request.LogonUserIdentity.Name);
            return PartialView("LegalEntitiesEdit", currentCustomer);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerLegalEntitiesAdd([ModelBinder(typeof(DevExpressEditorsBinder))] LegalEntities legalEntity, int customerId)
        {

          //  throw new NotImplementedException();

            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            //legalEntity.IdUser = Request.LogonUserIdentity.Name;
            legalEntity.IsActive = true;
            if (ModelState.IsValid)
            {
                if (!currentCustomer.LegalEntities.Contains(legalEntity))
                {
                    legalEntity.IdCustomer = idCustomer;
                    var leCol = new LegalEntitiesCollection();
                  if(leCol.Add(legalEntity, Request.LogonUserIdentity.Name))
                      return PartialView("LegalEntitiesEdit", currentCustomer);
                    return RedirectToAction("Error", "Home");
                }
            }

            return PartialView("LegalEntitiesEdit", currentCustomer);
        }

        public ActionResult CustomerLegalEntitiesDelete([ModelBinder(typeof(DevExpressEditorsBinder))] LegalEntities legalEntity, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
           // currentCustomer.RemoveLegalEntity(legalEntity);
           
            return PartialView("LegalEntitiesEdit", currentCustomer);
        }

        public ActionResult LegalEntitiesList()
        {
            var col = new LegalEntitiesCollection();
            return View(col.LegalEntitiesList);
        }

        public ActionResult LegalEntitiesListPartial()
        {
            var col = new LegalEntitiesCollection();
            return PartialView("LegalEntitiesListPartial", col.LegalEntitiesList);
        }

        public ActionResult FullLegalEntitiesList()
        {
            var col = new LegalEntitiesCollection();
            return View(col.LegalEntitiesList);
        }

        public ActionResult FullLegalEntitiesListPartial()
        {
            var col = new LegalEntitiesCollection();
            return PartialView("FullLegalEntitiesListPartial", col.LegalEntitiesList);
        }

        #endregion

        #region ContactsAddresses_Contacts

        public ActionResult CustomerAddressesAndContacts_Contacts_Callback(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("AddressesAndContacts_Contacts", currentCustomer);
        }

        public ActionResult CustomerAddressesAndContacts_Addresses_Callback_Edit(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("GetAddressesAndContacts_Address_Edit", currentCustomer);
        }


        public ActionResult CustomerAddressesAndContacts_Addresses_Callback(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("AddressesAndContacts_Address", currentCustomer);
        }

        public ActionResult CustomerAddressesAndContacts_Contacts_Edit_Callback(int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);

            return PartialView("AddressesAndContacts_Contacts_Edit", currentCustomer);
        }

        public ActionResult CustomerAddressesAndContacts_Contacts_EditUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] CRM.Database.Model.Contacts contacts, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            var contCol = new ContactsCollection();
            //Contacts ce4update = DB.GetContactById(contacts.IdContact);
            //ce4update.Update(contacts, Request.LogonUserIdentity.Name);
            if(!contCol.Update(contacts,Request.LogonUserIdentity.Name))
                return RedirectToAction("Error", "Home");
            return PartialView("AddressesAndContacts_Contacts_Edit", currentCustomer);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult CustomerAddressesAndContacts_Contacts_EditAdd([ModelBinder(typeof(DevExpressEditorsBinder))] CRM.Database.Model.Contacts contacts, int customerId)
        {
            var col = new CustomersCollection();

            var contCol = new ContactsCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            contacts.IdUser = Request.LogonUserIdentity.Name;
            if (ModelState.IsValid)
            {
                if (contCol.Add(contacts, Request.LogonUserIdentity.Name))
                 return PartialView("AddressesAndContacts_Contacts_Edit", currentCustomer);
                //    currentCustomer.AddContact(contacts,(int) contacts.IdLegalEntity,Request.LogonUserIdentity.Name);
            }

            return RedirectToAction("Error", "Home");
        }

        public ActionResult CustomerAddressesAndContacts_Contacts_EditDelete([ModelBinder(typeof(DevExpressEditorsBinder))] CRM.Database.Model.Contacts contacts, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            var contCol = new ContactsCollection();
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
           

            if(!contCol.Delete(contacts))
                return RedirectToAction("Error", "Home");
            return PartialView("AddressesAndContacts_Contacts_Edit", currentCustomer);
        }


#endregion

        #region Agreements
        public ActionResult GetAddressesAndContacts_Address_Edit(Customers customers)
        {
            return PartialView("GetAddressesAndContacts_Address_Edit", customers);
        }




        public ActionResult AgreementElementsUpdate([ModelBinder(typeof(DevExpressEditorsBinder))] AgreementElements agreementElements, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            var agrCol = new AgreementElementsCollection();
            agrCol.Update(agreementElements, Request.LogonUserIdentity.Name);
            return PartialView("AgreementElementsEdit", currentCustomer);
        }

        [HttpPost, ValidateInput(true)]
        public ActionResult AgreementElementsAdd([ModelBinder(typeof(DevExpressEditorsBinder))] AgreementElements agreementElements, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            agreementElements.IdUser = Request.LogonUserIdentity.Name;
            agreementElements.IsActive = true;
            if (TryValidateModel(agreementElements))
            {
                var agrCol = new AgreementElementsCollection();
                agrCol.Add(agreementElements, Request.LogonUserIdentity.Name);
            }

            return PartialView("AgreementElementsEdit", currentCustomer);
        }

        public ActionResult AgreementElementsDelete([ModelBinder(typeof(DevExpressEditorsBinder))] AgreementElements agreementElements, int customerId)
        {
            var col = new CustomersCollection();
            int idCustomer = customerId;
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            var agrCol = new AgreementElementsCollection();
            agrCol.Delete(agreementElements);

            return PartialView("AgreementElementsEdit", currentCustomer);
        }
        
        #endregion

        #region Addresses
        public ActionResult Country(Addresses address)
        {
            return PartialView(address);
        } 

        public ActionResult Region(Addresses address)
        {
            return PartialView(address);
        }


        public ActionResult City(Addresses address)
        {
            return PartialView(address);
        }

        public ActionResult profineRegion(Addresses address)
        {
            return PartialView(address);
        }

        public ActionResult CityCallback(int key)
        {
            var col =new SettlementsCollection();
            Settlements settlement = null;
            if (key > 0)
            {
                settlement = col.GetItemById(key) as Settlements;
            }

            var idRegion=-1;

            if (Request.Params["Region"] != null)
                if (Request.Params["Region"] != "null")
                idRegion = int.Parse(Request.Params["Region"]);

            //(Request.Params["Region"] != null) ? int.Parse(Request.Params["Region"]) : -1;
            if (idRegion == -1)
                idRegion = (int)settlement.IdRegion;
            if (idRegion > -1)
            {
                var regCol = new RegionsCollection();
                settlement =  ((Regions) regCol.GetItemById(idRegion)).Settlements.First();
              //  address.IdSettlement = address.Settlements.IdSettlement;
            }

            return PartialView("City", settlement);
        }

       public ActionResult regionCallback(int key)
       {
           Settlements settlement = null;
           if (key > 0)
           {
               var col = new SettlementsCollection();
               settlement = col.GetItemById(key) as Settlements;
           }
           var idprofineRegion = (Request.Params["profineRegion"] != null) ? int.Parse(Request.Params["profineRegion"]) : -1;
           if(idprofineRegion>-1)
           {
               var profineCol = new profineRegionsCollection();
               settlement =((profineRegions)profineCol.GetItemById(idprofineRegion)).Regions.First().Settlements.First();
            //   address.IdSettlement = address.Settlements.IdSettlement;
           }

           return PartialView("Region", settlement);
       }

        #endregion

        #region ProfuctSystems

        public ActionResult ProductSystemsList(int idCustomer)
        {
            var col = new CustomersCollection();
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("ProductSystemsList", currentCustomer);
        }

        public ActionResult ProductSystemsEdit(int idCustomer)
        {
            var col = new CustomersCollection();
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("ProductSystemsEdit", currentCustomer);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult CustomersProductSystemsAdd([ModelBinder(typeof(DevExpressEditorsBinder))] CustomersProductSystems item)
        {
            var col = new CustomersCollection();
            var eqCol = new CustomersProductSystemsCollection();
            eqCol.Add(item, Request.LogonUserIdentity.Name);
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(110968);
            return PartialView("ProductSystemsEdit", currentCustomer);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult CustomersProductSystemsDelete([ModelBinder(typeof(DevExpressEditorsBinder))] CustomersProductSystems item, int idCustomer)
        {
            var col = new CustomersCollection();
            var eqCol = new CustomersProductSystemsCollection();
            eqCol.Delete(item);
            CRM.Database.Model.Customers currentCustomer = (Database.Model.Customers)col.GetItemById(idCustomer);
            return PartialView("ProductSystemsEdit", currentCustomer);
        }

        #endregion

    }
}